import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

// 自定义异常类，表示余额不足
class InsufficientBalanceException extends Exception {
    public InsufficientBalanceException(String message) {
        super(message);
    }
}

// 银行账户类
class BankAccount {
    private String accountNumber;
    private double balance;

    public BankAccount(String accountNumber, double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    // Getters
    public String getAccountNumber() {
        return accountNumber;
    }

    public double getBalance() {
        return balance;
    }

    // 存款方法
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("存款成功，当前余额为：" + balance);
        } else {
            System.out.println("存款金额必须为正数");
        }
    }

    // 取款方法
    public void withdraw(double amount) throws InsufficientBalanceException {
        if (amount > 0) {
            if (amount <= balance) {
                balance -= amount;
                System.out.println("取款成功，当前余额为：" + balance);
            } else {
                throw new InsufficientBalanceException("余额不足");
            }
        } else {
            System.out.println("取款金额必须为正数");
        }
    }

    @Override
    public String toString() {
        return "账号: " + accountNumber + ", 余额: " + balance;
    }
}

public class Main {
    private static List<BankAccount> accounts = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        boolean isRunning = true;

        while (isRunning) {
            System.out.println("\n银行账户管理系统");
            System.out.println("1. 添加账户");
            System.out.println("2. 显示所有账户");
            System.out.println("3. 选择账户存钱");
            System.out.println("4. 选择账户提款");
            System.out.println("5. 退出");
            System.out.print("请输入选项：");

            try {
                int choice = scanner.nextInt();
                scanner.nextLine(); // 清除换行符

                switch (choice) {
                    case 1:
                        addAccount();
                        break;
                    case 2:
                        displayAccounts();
                        break;
                    case 3:
                        depositToAccount();
                        break;
                    case 4:
                        withdrawFromAccount();
                        break;
                    case 5:
                        isRunning = false;
                        System.out.println("系统已退出！");
                        break;
                    default:
                        System.out.println("无效选项！");
                }
            } catch (InputMismatchException e) {
                System.out.println("错误：请输入数字！");
                scanner.nextLine(); // 清除错误输入
            }
        }
        scanner.close();
    }

    private static void addAccount() {
        try {
            System.out.print("输入账号：");
            String accountNumber = scanner.nextLine();

            System.out.print("输入初始余额：");
            double balance = scanner.nextDouble();

            accounts.add(new BankAccount(accountNumber, balance));
            System.out.println("账户添加成功！");
        } catch (InputMismatchException e) {
            System.out.println("错误：初始余额必须为数字！");
            scanner.nextLine(); // 清除错误输入
        }
    }

    private static void displayAccounts() {
        if (accounts.isEmpty()) {
            System.out.println("暂无账户信息！");
            return;
        }
        for (BankAccount account : accounts) {
            System.out.println(account);
        }
    }

    private static void depositToAccount() {
        System.out.print("输入要存钱的账号：");
        String accountNumber = scanner.nextLine();
        BankAccount targetAccount = findAccount(accountNumber);
        if (targetAccount != null) {
            try {
                System.out.print("输入存款金额：");
                double amount = scanner.nextDouble();
                targetAccount.deposit(amount);
            } catch (InputMismatchException e) {
                System.out.println("错误：存款金额必须为数字！");
                scanner.nextLine(); // 清除错误输入
            }
        } else {
            System.out.println("账号不存在！");
        }
    }

    private static void withdrawFromAccount() {
        System.out.print("输入要提款的账号：");
        String accountNumber = scanner.nextLine();
        BankAccount targetAccount = findAccount(accountNumber);
        if (targetAccount != null) {
            try {
                System.out.print("输入提款金额：");
                double amount = scanner.nextDouble();
                targetAccount.withdraw(amount);
            } catch (InputMismatchException e) {
                System.out.println("错误：提款金额必须为数字！");
                scanner.nextLine(); // 清除错误输入
            } catch (InsufficientBalanceException e) {
                System.out.println("异常：" + e.getMessage());
            }
        } else {
            System.out.println("账号不存在！");
        }
    }

    private static BankAccount findAccount(String accountNumber) {
        for (BankAccount account : accounts) {
            if (account.getAccountNumber().equals(accountNumber)) {
                return account;
            }
        }
        return null;
    }
}